/********************************************************************************
 * ShowDynamicTreeAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Jan 5, 2009 6:34:06 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.CategoryManagerServiceDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.util.AdminConstants;


public class ShowDynamicTreeAction extends FrontBaseSupport {

	public ShowDynamicTreeAction() {
		super(false, false);
		// TODO Auto-generated constructor stub
	}

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private Map<Integer, GeneralCategories> treeMap = new HashMap<Integer, GeneralCategories>();

	private List<GeneralCategories> tempGeneralCategoriesList = new ArrayList<GeneralCategories>();

	private Map<Integer, LocationCategories> treeLocationMap = new HashMap<Integer, LocationCategories>();

	private List<LocationCategories> locationList = new ArrayList<LocationCategories>();

    public List<GeneralCategories> getTempGeneralCategoriesList() {
		return tempGeneralCategoriesList;
	}

	public void setTempGeneralCategoriesList(
			List<GeneralCategories> tempGeneralCategoriesList) {
		this.tempGeneralCategoriesList = tempGeneralCategoriesList;
	}

	@Override
    public String execute() throws Exception {
    	tempGeneralCategoriesList = CategoryManagerServiceDelegator.getParentList();
    	for (Iterator iterator = tempGeneralCategoriesList.iterator(); iterator.hasNext();) {
			GeneralCategories category = (GeneralCategories) iterator.next();
			category = CategoryManagerServiceDelegator.removeParentfromSet(category);
			treeMap.put(category.getCategoryId(), category);
		}
    	return SUCCESS;
    }

	public String showDynamicRegionTreeAction() throws Exception {
		locationList = LocationCategoriesServiceDelegator.getParentList();
    	for (Iterator iterator = locationList.iterator(); iterator.hasNext();) {
    		LocationCategories category = (LocationCategories) iterator.next();
			category = LocationCategoriesServiceDelegator.removeParentfromSet(category);
			treeLocationMap.put(category.getCategoryId(), category);
		}
    	return SUCCESS;
	}

    public GeneralCategories getGeneralCategoriesFromCategoryId(int categoryid){
    	try{
			tempGeneralCategoriesList = (List<GeneralCategories>) ActionContext.getContext().getApplication().get(AdminConstants.GENERALCATEGORIES);
			GeneralCategories type = new GeneralCategories();

			for (Iterator<GeneralCategories> iterator = tempGeneralCategoriesList.iterator(); iterator.hasNext();) {
				type = (GeneralCategories) iterator.next();
				if(categoryid == type.getCategoryId()){
					return type;
				}
			}
		 }catch (Exception e) {
			 addActionError(e.getMessage());
		 }

		 return null;
	}

	public Map<Integer, GeneralCategories> getTreeMap() {
		return treeMap;
	}
	public void setTreeMap(Map<Integer, GeneralCategories> treeMap) {
		this.treeMap = treeMap;
	}

	public Map<Integer, LocationCategories> getTreeLocationMap() {
		return treeLocationMap;
	}

	public void setTreeLocationMap(Map<Integer, LocationCategories> treeLocationMap) {
		this.treeLocationMap = treeLocationMap;
	}

	public List<LocationCategories> getLocationList() {
		return locationList;
	}

	public void setLocationList(List<LocationCategories> locationList) {
		this.locationList = locationList;
	}
}

